#!/usr/bin/env python
# -*- coding: utf-8 -*-

#                         TODO:
# Make  - for tasks
#

tasklist = []
version = '0.0.3'
#
#  Menu def`s
#
def TaskManager():
	global tasklist
	loop = True
	while (loop == True):		
		menu = raw_input('> ')
		
		if menu == 'help': #help
			show_help()
		elif menu == 'ls':                                   #all tasks
			show_tasks('no_arg')
		elif menu[0:3] == 'ls ':                       #tasks whith arg
			show_tasks(menu[3:])
		elif menu[0:4] == 'add ':                             #add task
			add_task(menu[4:])
		elif menu[0:3] == 'rm ':                  #remove task by index
			remove_task(menu[3:])
		elif menu[0:7] == 'search ': #search (like "ls arg" but more powerful)
			search_task(menu[7:])
		elif menu[0:2] == '+ ':                          #complate task
			complate(menu[2:])
		elif menu == 'exit' or menu == '0':                       #exit
			loop = False
		else:
			print 'Error'

def show_tasks(arg):                                 #list task
	global tasklist
	
	if arg == 'no_arg':                                       #show all
		for i in range(1, len(tasklist)):
			task = tasklist[i]
			print i, task[0], task[1:len(task)-1]
	elif arg[0] == '#':                                   # list by tag
		for i in range(1, len(tasklist)):
			task = tasklist[i]
			if task.find(arg) >= 0:
				print i, task[0], task[1:len(task)-1]
	elif arg[0] == '+':                                  #list complate
		for i in range(1, len(tasklist)):
			task = tasklist[i]
			if task[0] == '+':
				print i, task[0], task[1:len(task)-1]
	elif arg[0] == '-':                                #list incomplate
		for i in range(1, len(tasklist)):
			task = tasklist[i]
			if task[0] == '-':
				print i, task[0], task[1:len(task)-1]
	else:
		print 'List error'	
	print ''

def add_task(task):
	global tasklist
	task = '-' + task + '\n'
	tasklist.append(task)

def remove_task(arg):
	global tasklist
	tasklist.pop(int(arg))
	show_tasks('no_arg')
	
def search_task(arg):
	global tasklist
	count = 0
	if arg == '':
		print 'Search error'	
	else:
		for i in range(1, len(tasklist)):
			task = tasklist[i]
			if task.find(arg) >= 0:
				count += 1
				print i, task[0], task[1:len(task)-1]
		if count == 0:
			print '"%s" was not found' % arg
		
	print ''
	
def complate(arg):
	task = tasklist[int(arg)]
	if task[0] == '-':
		task = '+' + task[1:]
		tasklist[int(arg)] = task
		show_tasks('no_arg')
	else:
		print 'Task already +'


#
# Help
#
def show_help():
	print 'ToDo.py %s' % version
	print 'Copyright(c) 2011 kemmotar'
		# HELP INFO
	print """	
	lst                   - list all tasks
	lst #tag              - list all with #tag
	lst +                 - 
	add task              -
	rmv index             -
	exit                  -
	"""

#	print '\nType "back" to exit from help'
#	back_loop = True
#	while back_loop == True:
#			back_var = raw_input('> ')
#			if back_var == 'back':
#				loop = False
#				back_loop = False
#	print '\nToDo.py %s' % version
#	print 'Type "help" for more information'
#			else:
#				print 'Error'

#
#  Main
#
def main():
	global tasklist

	# Open file and read lines
	with open('todo.txt', 'r') as todo:
		tasklist = todo.readlines()
	todo.close()
	
	if tasklist[0] != 'Created by ToDo.py\n':
		tasklist.insert(0, 'Created by ToDo.py\n')
	for i in range(1, len(tasklist)):
			task = tasklist[i]
			if task[0] != '-' and task[0] != '+':
				task = '-' + task[0:]
				tasklist[i] = task
	
	# Starting text
	print 'ToDo.py %s' % version
	print 'Type "help" for more information'

	# Functions
	TaskManager()

	# Write file
	save = raw_input('Do you want save changes? ("yes" or "no")\n> ')
	if save == 'yes':
		with open('todo.txt', 'w') as todo:
			todo.writelines(tasklist)
		todo.close()
	else:
		print ('Changes are not saved')	
	# Quiting
	print 'Thx for using =)'	

	return 0

if __name__ == '__main__':
	main()
